package czh.cocoa.ui.impl;

import czh.cocoa.foundation.NSArray;
import czh.cocoa.foundation.NSMutableArray;
import czh.cocoa.foundation.NSObject;
import czh.cocoa.foundation.NSSelector;
import czh.cocoa.foundation.NSString;
import czh.cocoa.graphics.CGRect;
import czh.cocoa.graphics.CGSize;
import czh.cocoa.ui.UIAnimationBlock;
import czh.cocoa.ui.UIColor;
import czh.cocoa.ui.UIEdgeInsets;
import czh.cocoa.ui.UIFont;
import czh.cocoa.ui.UIImage;
import czh.cocoa.ui.UILineBreakMode;
import czh.cocoa.ui.UIScreen;
import czh.cocoa.ui.UITextAlignment;
import czh.cocoa.ui.UIView;

import static czh.cocoa.ui.impl.UIControl.*;
import static czh.cocoa.ui.impl.UIBarButtonItem.*;

public class UINavigationBar extends UIView {

	public final static int UIBarStyleDefault = 0;
	public final static int UIBarStyleBlack = 1;
	
	private int mBarStyle;
	
	private NSMutableArray mItems;
	
	private NSMutableArray mContentViews;
	
	private UIImageView mBackgroundView;
	
	private UINavigationBarDelegate mDelegate;
	
	public UINavigationBar() {
		
	}
	
	protected void init() {
		super.init();
		mItems = new NSMutableArray(3);
		mContentViews = new NSMutableArray(3);
		
		float bar_width = UIScreen.mainScreen().bounds().size.width;
		setFrame(CGRectMake(0, 0, bar_width, DIP(88))); 
		
		UIImageView bgview = new UIImageView(bounds());
		bgview.setAutoresizingMask(UIViewAutoresizingFlexibleSize);
		addSubview(bgview);
		mBackgroundView = bgview;
		
		setBarStyle(UIBarStyleDefault);
	}
	
	public void setDelegate(UINavigationBarDelegate delegate) {
		mDelegate = delegate;
	}
	
	public UINavigationBarDelegate delegate() {
		return mDelegate;
	}
	
	public void setBarStyle(int style) {
		mBarStyle = style;
		UIImage backgoround;
		if (style == UIBarStyleDefault) {
			backgoround = UIImage.imageNamed(NSTR("cocoa/navi_bar_default")); 
		} else {
			backgoround = UIImage.imageNamed(NSTR("cocoa/navi_bar_black"));
		}
		mBackgroundView.setImage(backgoround);
		
		for (NSObject o : mContentViews) {
			ContentView view = (ContentView) o;
			view.setBarStyle(style); 
		}
	}
	
	public int barStyle() {
		return mBarStyle;
	}
	
	public NSArray items() {
		return new NSArray(mItems);
	}
	
	public void setItems(NSArray items) {
		mItems.removeAllObjects();
		mItems.addObjectsFromArray(items);
		
		for (NSObject o : mContentViews) {
			UIView view = (UIView) o;
			view.removeFromSuperview();
		}
		
		mContentViews.removeAllObjects();
		
		for (int i = 0; i < items.count(); i++) {
			UINavigationItem item = (UINavigationItem) items.objectAtIndex(i); 
			ContentView view = new ContentView(bounds());
			view.setNavigationItem(item);
			mContentViews.addObject(view);
			
			if (i == items.count() - 1) { 
				addSubview(view);
			}
		}
		
	}
	
	public void pushNavigationItemAnimated(UINavigationItem item, boolean animated) {
		mItems.addObject(item);
		
		final ContentView view = new ContentView(bounds());
		view.setNavigationItem(item);
		mContentViews.addObject(view);
		
		final int count = mContentViews.count();
		if (count > 1) {
			UINavigationItem item0 = (UINavigationItem) mItems.objectAtIndex(count - 2);
			final UIView view0 = (UIView) mContentViews.objectAtIndex(count - 2);
			//view0.removeFromSuperview();
			
			if (view.mLeftButton == null) {
				view.setBackButtonVisible(true); 
				view.setBackButtonTitle(item0.title()); 
			}
			
			if (animated) {
				final CGRect frame = view.frame();
				frame.origin.x = frame.size.width;
				view.setFrame(frame); 
				addSubview(view); 
				
				final CGRect frame0 = view0.frame();
				
				UIView.animateWithDurationAnimationsCompletion(0.3, new UIAnimationBlock() {
					@Override
					public void run() {
						frame.origin.x = 0;
						view.setFrame(frame); 
						frame0.origin.x = -frame0.size.width;
						view0.setFrame(frame0);
					}
				}, new UIAnimationBlock.Completion() {
					@Override
					public void run(boolean finished) {
						frame0.origin.x = 0;
						view0.setFrame(frame0);
						view0.removeFromSuperview();
					}
				});
				
			} else {
				
				addSubview(view); 
				view0.removeFromSuperview();
			}
			
		} else {
			
			addSubview(view);
			
		}
		
		
		
	}
	
	public UINavigationItem popNavigationItemAnimated(boolean animated) {
		final int count = mItems.count();
		if (count > 0) {
			UINavigationItem item = (UINavigationItem) mItems.objectAtIndex(count - 1);
			mItems.removeObjectAtIndex(count - 1);
			
			final UIView view = (UIView) mContentViews.objectAtIndex(count - 1);
			mContentViews.removeObjectAtIndex(count - 1);
			
			if (count > 1) {
				final UIView view0 = (UIView) mContentViews.objectAtIndex(count - 2);
				
				if (animated) {
					
					final CGRect frame0 = view0.frame();
					frame0.origin.x = -frame0.size.width;
					view0.setFrame(frame0);
					addSubview(view0);
					
					final CGRect frame = view.frame();
					
					UIView.animateWithDurationAnimationsCompletion(0.3, new UIAnimationBlock() {
						@Override
						public void run() {
							frame0.origin.x = 0;
							view0.setFrame(frame0); 
							frame.origin.x = frame.size.width;
							view.setFrame(frame);
						}
					}, new UIAnimationBlock.Completion() {
						@Override
						public void run(boolean finished) {
							frame.origin.x = 0;
							view.setFrame(frame);
							view.removeFromSuperview();
						}
					});
					
				} else {
					
					addSubview(view0);
					view.removeFromSuperview();
					
				}
				
			} else {
				
				view.removeFromSuperview();
				
			}
			
			return item;
		}
		return null;
	}
	
	private class ContentView extends UIView {
		
		private UILabel mTitleView;
		
		private UIButton mBackButton;

		private UIButton mRightButton;
		
		private UIButton mLeftButton;
		
		private UINavigationItem mNaviItem;
		
		public ContentView(CGRect frame) {
			super(frame);
		}
		
		public void setBackButtonVisible(boolean visible) {
			mBackButton.setHidden(!visible);
		}
		
		public void setBackButtonTitle(NSString title) {
			if (title == null) {
				title = NSTR("返回"); 
			}
			mBackButton.setTitleForState(title, UIControlStateNormal); 
			
			UIFont font = mBackButton.font();
			CGRect frame = mBackButton.frame();
			//UIEdgeInsets insets = mBackButton.titleEdgeInsets();
			float padding = 16;
			float max_width = mTitleView.frame().origin.x - frame.origin.x - padding - 5;
			CGSize size = title.sizeWithFontForWidthLineBreakMode(font, max_width, UILineBreakMode.TailTruncation);
			float width = size.width + padding;
			if (width > 72) {
				NSLog("large than 72", "");
				frame.size.width = width + 12;
				mBackButton.setFrame(frame);
				mBackButton.setTitleEdgeInsets(UIEdgeInsetsMake(0, padding, 0, 0)); 
			}
		}
		
		public void setRightButtonTitle(NSString title) {
			if (title == null) {
				title = NSTR(""); 
			}
			mRightButton.setTitleForState(title, UIControlStateNormal); 
			
			UIFont font = mRightButton.font();
			CGRect frame = mRightButton.frame();

			CGRect title_frame = mTitleView.frame();
			float max_width = title_frame.origin.x + title_frame.size.width - 15;
			CGSize size = title.sizeWithFontForWidthLineBreakMode(font, max_width, UILineBreakMode.TailTruncation);
			if (size.width > 72) {
				frame.size.width = size.width;
				frame.origin.x = bounds().size.width - size.width - 10;
				mRightButton.setFrame(frame);
			}
		}
		
		public void setLeftButtonTitle(NSString title) {
			if (title == null) {
				title = NSTR(""); 
			}
			mLeftButton.setTitleForState(title, UIControlStateNormal); 
			
			UIFont font = mLeftButton.font();
			CGRect frame = mLeftButton.frame();

			CGRect title_frame = mTitleView.frame();
			float max_width = title_frame.origin.x - 15;
			CGSize size = title.sizeWithFontForWidthLineBreakMode(font, max_width, UILineBreakMode.TailTruncation);
			if (size.width > 72) {
				frame.size.width = size.width;
				mLeftButton.setFrame(frame);
			}
		}
		
		public void setBarStyle(int style) {
			UIImage backbg, backbg2;
			if (style == UIBarStyleDefault) {
				backbg = UIImage.imageNamed(NSTR("cocoa/navi_back_default")); 
				backbg2 = UIImage.imageNamed(NSTR("cocoa/navi_back_default_highlighted")); 
			} else {
				backbg = UIImage.imageNamed(NSTR("cocoa/navi_back_black"));
				backbg2 = UIImage.imageNamed(NSTR("cocoa/navi_back_black_highlighted"));
			}
			mBackButton.setBackgroundImageForState(backbg, UIControlStateNormal);
			mBackButton.setBackgroundImageForState(backbg2, UIControlStateHighlighted);
			
			if (mRightButton != null || mLeftButton != null) {
				UIImage btnbg, btnbg2;
				if (mNaviItem.rightBarButtonItem().style() == UIBarButtonItemStyleDone) {
					btnbg = UIImage.imageNamed(NSTR("cocoa/navi_btn_done"));
					btnbg2 = UIImage.imageNamed(NSTR("cocoa/navi_btn_done_highlighted"));
				} else {
					if (style == UIBarStyleDefault) {
						btnbg = UIImage.imageNamed(NSTR("cocoa/navi_btn_plain_default"));
						btnbg2 = UIImage.imageNamed(NSTR("cocoa/navi_btn_done_highlighted"));
					} else {
						btnbg = UIImage.imageNamed(NSTR("cocoa/navi_btn_plain_black"));
						btnbg2 = UIImage.imageNamed(NSTR("cocoa/navi_btn_plain_black_highlighted"));
					}
				}
				if (mRightButton != null) {
					mRightButton.setBackgroundImageForState(btnbg, UIControlStateNormal);
					mRightButton.setBackgroundImageForState(btnbg2, UIControlStateHighlighted);	
				}
				if (mLeftButton != null) {
					mLeftButton.setBackgroundImageForState(btnbg, UIControlStateNormal);
					mLeftButton.setBackgroundImageForState(btnbg2, UIControlStateHighlighted);	
				}
			}
		}

		public void setNavigationItem(final UINavigationItem item) {
			mNaviItem = item;
			final CGSize bar_size = bounds().size; 
			
			float title_width = bar_size.width / 2;
			UILabel title_view = new UILabel(CGRectMake(0, 0, title_width, 50)); 
			title_view.setCenter(center()); 
			title_view.setTextAlignment(UITextAlignment.Center);
			title_view.setTextColor(UIColor.whiteColor());
			title_view.setFont(UIFont.boldSystemFontOfSize(23));
			title_view.setText(item.title()); 
			//title_view.setBackgroundColor(UIColor.yellowColor());
			addSubview(title_view);
			mTitleView = title_view;
			
			float back_btn_height = DIP(60); 
			float back_btn_top = (bar_size.height - back_btn_height) / 2;
			UIButton back_btn = new UIButton(CGRectMake(10, back_btn_top, DIP(90), back_btn_height)); 
			back_btn.setTitleEdgeInsets(UIEdgeInsetsMake(0, 5, 0, 0));
			back_btn.setTitleColorForState(UIColor.whiteColor(), UIControlStateNormal);  
			back_btn.setHidden(true);
			back_btn.setContentStretch(CGRectMake(0.7f, 0, 0.1f, 1)); 
			//back_btn.titleLabel().setTextAlignment(UITextAlignment.Left);
			//back_btn.titleLabel().setBackgroundColor(UIColor.orangeColor());
			addSubview(back_btn);
			mBackButton = back_btn;
			
			final UINavigationBar target = UINavigationBar.this;
			back_btn.addTargetActionForControlEvents(target, new NSSelector() {
				@Override
				public void perform(NSObject receiver, NSObject params) {
					boolean pop = true;
					if (mDelegate != null) {	
						pop = mDelegate.navigationBarShouldPopItem(UINavigationBar.this, item);
					}
					if (pop) {
						target.popNavigationItemAnimated(true);
					}
				}
			}, UIControlEventTouchUpInside);
			
			UIBarButtonItem right_item = item.rightBarButtonItem();
			if (right_item != null) {
				float right_btn_width = DIP(90); 
				float right_btn_height = DIP(60); 
				float right_btn_left = bar_size.width - right_btn_width - 10;
				float right_btn_top = (bar_size.height - right_btn_height) / 2;
				CGRect right_btn_frame = CGRectMake(right_btn_left, right_btn_top, 
													right_btn_width, right_btn_height);
				UIButton right_btn = new UIButton(right_btn_frame); 
				right_btn.setTitleColorForState(UIColor.whiteColor(), UIControlStateNormal);
				right_btn.setContentStretch(CGRectMake(0.5f, 0, 0.1f, 1)); 
				addSubview(right_btn);
				mRightButton = right_btn; 
				
				if (right_item.target() != null) { 
					right_btn.addTargetActionForControlEvents(right_item.target(), 
							right_item.action(), UIControlEventTouchUpInside);
				}
				
				UIImage image = right_item.image();
				if (image != null) { 
					right_btn.setImageForState(image, UIControlStateNormal);
					right_btn.imageView().setContentMode(UIViewContentModeScaleAspectFit);
				} else {
					setRightButtonTitle(right_item.title());
				}

			}
			
			UIBarButtonItem left_item = item.leftBarButtonItem();
			if (left_item != null) {
				float left_btn_width = DIP(90);
				float left_btn_height = DIP(60); 
				float left_btn_left = 10;
				float left_btn_top = (bar_size.height - left_btn_height) / 2;
				CGRect left_btn_frame = CGRectMake(left_btn_left, left_btn_top, 
													left_btn_width, left_btn_height);
				UIButton left_btn = new UIButton(left_btn_frame); 
				left_btn.setTitleColorForState(UIColor.whiteColor(), UIControlStateNormal);
				left_btn.setContentStretch(CGRectMake(0.5f, 0, 0.1f, 1)); 
				addSubview(left_btn);
				mLeftButton = left_btn; 
				
				if (left_item.target() != null) { 
					left_btn.addTargetActionForControlEvents(left_item.target(), 
							left_item.action(), UIControlEventTouchUpInside);
				}
				
				setLeftButtonTitle(left_item.title());
			}
			
			setBarStyle(mBarStyle);
		}
		
	}
	
}
